group: Fix interrupt_exist_connections for routed connections#4285
Open
xxspa wants to merge 4 commits into
Open
group: Fix interrupt_exist_connections for routed connections#4285xxspa wants to merge 4 commits into
xxspa wants to merge 4 commits into
Conversation
Add SingPacketConn and Group.NewSingPacketConn to allow registering N.PacketConn connections in an interrupt group, mirroring the existing net.Conn and net.PacketConn wrappers.
11776c6 to
09e819f
Compare
Connections routed to a selector (route target or final) enter through the ConnectionHandler path (NewConnection/NewPacketConnection), which never goes through Selector.DialContext/ListenPacket, so they were never registered in the interrupt group. As a result, SelectOutbound interrupted nothing and existing connections kept flowing over the previously selected outbound. Register the inbound connection in the interrupt group as an external connection on both handler branches, so switching the selected outbound interrupts routed connections when interrupt_exist_connections is enabled, while leaving them untouched by default. Fixes SagerNet#4281.
09e819f to
07460d1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4281.
Root cause
Connections routed to a selector (as a route target or
route.final) enter through theConnectionHandlerpath (Selector.NewConnection/NewPacketConnection). Neither branch of that path goes throughSelector.DialContext/ListenPacket, so these connections were never registered in the selector's interrupt group.SelectOutboundthen interrupted an empty set, and existing connections kept flowing over the previously selected outbound — makinginterrupt_exist_connectionsa near no-op for user traffic.URLTest is not affected on this path because its
NewConnectionpasses itself as the dialer, so dialing goes throughURLTest.DialContext, which registers the connection.Fix
NewConnection/NewPacketConnection, covering both the nested-handler branch and the connection-manager branch. Simply passing the selector as the dialer (URLTest-style) would not be enough: members that only implementConnectionHandler(e.g. the dns outbound) need the handler branch, and connections handled by nested group members are only registered in the inner group.SingPacketConn/Group.NewSingPacketConntocommon/interruptso anN.PacketConncan be registered, mirroring the existing wrappers (withUpstream/ReaderReplaceable/WriterReplaceablepass-through).Tests
New end-to-end tests in
test/group_test.go(full box instance, SOCKS inbound, selector asroute.final, direct members, local echo servers):TestSelectorInterruptRoutedConnection: an established TCP connection must be closed afterSelectOutboundwheninterrupt_exist_connectionsis enabled — failed before this fix (read timed out), passes now.TestSelectorInterruptRoutedPacketConnection: a UDP session must be re-established over a new outbound socket after switching — failed before this fix (same source observed by the echo server), passes now.TestSelectorKeepRoutedConnection: withinterrupt_exist_connectionsdisabled, existing connections must survive switching — passes before and after, guarding the default semantics.Note: the test module's
go.mod/go.sumare currently behind the main module (missing entries forsing-tun/gtcpip/checksumimported bycommon/tlsspoof), so running the tests requires ago mod tidyintest/first; that sync is intentionally not included in this PR.